const express = require('express')
const validate = require('express-validation')
const _ = require('lodash')
const moment = require('moment')
const goodController = require('../controller/goodController')
const tokenChecker = require('../util/tokenChecker')

const goodRouter = express.Router()
const countPerPage = 30

goodRouter.get('/getGoods', async (req, res, next) => {
  // 分页，每页30个商品
  try {
    const goods = await goodController.getGoods()
    res.json({
      status: 200,
      msg: '获取商品成功',
      data: {
        result: goods
      }
    })
  } catch (e) {
    next(e)
  }
})

// goodRouter.get('/pageCount', tokenChecker, async (req, res, next) => {
//   // 分页，每页30个商品
//   try {
//     const count = await goodController.count()
//     const pageCount = Math.ceil(count / countPerPage)
//     res.json({ status: 200, msg: '商品数量', data: { pageCount } })
//   } catch (e) {
//     next(e)
//   }
// })

goodRouter.post('/upsert', tokenChecker, async (req, res, next) => {
  const { good } = req.good
  try {
    await goodController.upsertGood(good)
    res.json({ status: 200, msg: '保存商品成功', data: {} })
  } catch (e) {
    next(e)
  }
})

goodRouter.post('/delete', tokenChecker, async (req, res, next) => {
  const { goods } = req.goods
  try {
    await goodController.delete(goods)
    res.json({ status: 200, msg: '删除商品成功', data: {} })
  } catch (e) {
    next(e)
  }
})

module.exports = goodRouter
